/**
 * 用户管理关键字模块控制层
 *
 * @author JINMO
 * @since 2022/06/26 19:07
 */

package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.admin.AdminKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("admin/keyword")
public class AdminKeywordController {

    @Autowired
    AdminKeywordService adminKeywordService;

    /**
     * 通过分页参数查找关键词并返回
     *
     * @param keyword   关键字
     * @param url       url
     * @param baseParam 基本参数
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 19:23:28
     */
    @RequestMapping(value = "list")
    public BaseRespVo list(String keyword, String url, BaseParam baseParam) {

        CommonData<MarketKeyword> data = adminKeywordService.queryKeywordByBaseParamAndKeywordAndUrl(keyword, url, baseParam);

        return BaseRespVo.ok(data);
    }

    /**
     * 创建关键词
     *
     * @param marketKeyword 市场关键字
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 19:51:26
     */
    @RequestMapping(value = "create")
    public BaseRespVo create(@RequestBody MarketKeyword marketKeyword) {
        if (StringUtils.isEmpty(marketKeyword.getKeyword())) {
            return BaseRespVo.error("关键词不能为空");
        }

        marketKeyword.setKeyword(marketKeyword.getKeyword().trim());


        List<MarketKeyword> marketKeywords = adminKeywordService.selectAllKeywordByKeywordName(marketKeyword.getKeyword());
        if (marketKeywords.size() > 0) {
            return BaseRespVo.error("关键词不能重复");
        }

        MarketKeyword data = adminKeywordService.createKeyword(marketKeyword);

        return BaseRespVo.ok(data);
    }

    /**
     * 更新关键词
     *
     * @param marketKeyword 市场关键字
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 20:05:33
     */
    @RequestMapping(value = "update")
    public BaseRespVo update(@RequestBody MarketKeyword marketKeyword) {
        if (StringUtils.isEmpty(marketKeyword.getKeyword())) {
            return BaseRespVo.error("关键词不能为空");
        }

        marketKeyword.setKeyword(marketKeyword.getKeyword().trim());


        List<MarketKeyword> marketKeywords = adminKeywordService.selectAllKeywordByKeywordName(marketKeyword.getKeyword());
        if (marketKeywords.size() > 0) {
            return BaseRespVo.error("关键词不能重复");
        }



        MarketKeyword data = adminKeywordService.updateKeyword(marketKeyword);

        return BaseRespVo.ok(data);
    }

    /**
     * 删除关键词（修改删除属性为true）
     *
     * @param marketKeyword 市场关键字
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/26 20:17:38
     */
    @RequestMapping(value = "delete")
    public BaseRespVo delete(@RequestBody MarketKeyword marketKeyword) {

        adminKeywordService.deleteKeyword(marketKeyword);

        return BaseRespVo.ok();
    }
}
